﻿using System;
using System.Xml;
using System.Text;
using System.Linq;
using System.Collections.Generic;
using StripeOne.Core.S1Tools;

namespace StripeOne.Core.XmlConfig
{
	public class XmlObject
	{
		public string Section { get; set; }
		
		public XmlObject() : this(String.Empty) { }

		public XmlObject(string section)
		{
			this.Section = section;
			this.Id = 0;
		}

		public virtual List<T> GetXmlObjects<T>(string session, string path) where T : XmlObject
		{
			return XmlObjectTool.GetXmlObjects<T>(this.XmlElement, session, path);
		}

		public virtual T GetXmlObject<T>(string session) where T : XmlObject
		{
			return XmlObjectTool.GetXmlObject<T>(this.XmlElement, session);
		}

		#region ParseFields

		private string GetField(string fieldname)
		{
			string value = S1Parser.ToString(XmlObjectTool.GetField(this.XmlElement, fieldname));
			if(String.IsNullOrEmpty(value) && (this.XmlElement != null && this.XmlElement.HasAttribute(fieldname)))
			{
				value = S1Parser.ToString(this.XmlElement.GetAttribute(fieldname));
			}
			return value;
		}
		public virtual string GetXmlAttributeValue(string attributeName)
		{
			return this.GetXmlAttributeValue(this.Section, attributeName);
		}
        public virtual string GetXmlAttributeValue(string section, string attributeName)
		{
			string value = String.Empty;
			XmlElement xmlElement = XmlTool.GetXmlElement(this.XmlElement, S1Parser.ToString(section, this.Section));
			if(xmlElement != null)
			{
				if(xmlElement.HasAttribute(attributeName))
				{
					value = S1Parser.ToString(xmlElement.GetAttribute(attributeName));
				}
			}
			return value;
		}
        public virtual string GetXmlAttributeValue(string section, string attributeName, string defaultValue)
		{
			return S1Parser.ToString(this.GetXmlAttributeValue(section, attributeName), defaultValue);
		}
		public virtual string GetString(string fieldname)
		{
			return S1Parser.ToString(this.GetField(fieldname));
		}
		public virtual string GetString(string fieldname, string defaultValue)
		{
			return S1Parser.ToString(this.GetField(fieldname), defaultValue);
		}
		public virtual int GetInt32(string fieldname)
		{
			return S1Parser.ToInt32(this.GetField(fieldname));
		}
		public virtual int GetInt32(string fieldname, int defaultValue)
		{
			return S1Parser.ToInt32(this.GetField(fieldname), defaultValue);
		}
		public virtual bool GetBoolean(string fieldname)
		{
			return S1Parser.ToBoolean(this.GetField(fieldname));
		}
		public virtual bool GetBoolean(string fieldname, bool defaultValue)
		{
			return S1Parser.ToBoolean(this.GetField(fieldname), defaultValue);
		}
		public virtual float GetFloat(string fieldname)
		{
			return S1Parser.ToFloat(this.GetField(fieldname));
		}
		public virtual float GetFloat(string fieldname, float defaultValue)
		{
			return S1Parser.ToFloat(this.GetField(fieldname), defaultValue);
		}
		public virtual double GetDouble(string fieldname)
		{
			return S1Parser.ToDouble(this.GetField(fieldname));
		}
		public virtual double GetDouble(string fieldname, double defaultValue)
		{
			return S1Parser.ToDouble(this.GetField(fieldname), defaultValue);
		}
		public virtual decimal GetDecimal(string fieldname)
		{
			return this.GetDecimal(fieldname);
		}
		public virtual decimal GetDecimal(string fieldname, decimal defaultValue)
		{
			return S1Parser.ToDecimal(this.GetField(fieldname), defaultValue);
		}
		public virtual decimal GetDecimal(string fieldname, string cultureName)
		{
			return S1Parser.ToDecimal(this.GetField(fieldname), cultureName);
		}
		public virtual decimal GetDecimal(string fieldname, string cultureName, decimal defaultValue)
		{
			return S1Parser.ToDecimal(this.GetField(fieldname), cultureName, defaultValue);
		}
		public virtual DateTime GetDateTime(string fieldname)
		{
			DateTime date = S1Parser.ToDateTime(this.GetField(fieldname));
			return date;
		}
		public virtual DateTime GetDateTime(string fieldname, string cultureName)
		{
			return S1Parser.ToDateTime(this.GetField(fieldname), cultureName);
		}
		public virtual DateTime GetDateTime(string fieldname, string cultureName, DateTime defaultValue)
		{
			return S1Parser.ToDateTime(this.GetField(fieldname), cultureName, defaultValue);
		}

		#endregion

		public virtual string PartialView { get { return String.Empty; } }

        public virtual XmlElement XmlElement { get; set; }

		private int _id = 0;
		public virtual int Id
		{
			get
			{
				if(this._id.Equals(0))
				{
					this._id = (
						this.GetInt32(this.Section + "_id").Equals(0) ?
						this.GetInt32("id") :
						this.GetInt32(this.Section + "_id")
					);
				}
				return this._id;
			}
			set
			{
				this._id = value;
			}
		}
	}
}
